<template>
  <div>
    <!-- 关键生产设备 -->
    <basic-container shadow="hover">
      <curd-title :title="'关键生产设备'" />
      <avue-crud
        :table-loading="equipmentLoading"
        :option="equipmentOption"
        :data="equipmentList"
        @on-load="onEquipmentLoad"
        @row-save="rowEquipmentSave"
        @row-update="rowEquipmentUpdate"
        @refresh-change="refreshEquipmentChange"
        @row-del="rowEquipmentDel"
      >
      </avue-crud>
    </basic-container>
    <!-- 物流能力 -->
    <basic-container shadow="hover">
      <curd-title :title="'物流能力'" />
      <avue-crud
        :table-loading="logisticsLoading"
        :option="logisticsOption"
        :data="logisticsList"
        @on-load="onLogisticsLoad"
        @row-save="rowLogisticsSave"
        @row-update="rowLogisticsUpdate"
        @refresh-change="refreshLogisticsChange"
        @row-del="rowLogisticsDel"
      >
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import CurdTitle from '@/components/base/title'
import { equipmentOption, logisticsOption } from '@/option/tenant/supplier/equipment-info'
import { getEquipmentList, removeEquipment, addEquipment, updateEquipment } from '@/api/supplier/equipment'
import { getLogisticsList, removeLogistics, addLogistics, updateLogistics } from '@/api/supplier/logistics'
export default {
  name: 'EquipmentInfo',
  components: { CurdTitle },
  inject: ['id'],
  data() {
    return {
      equipmentList: [],
      equipmentLoading: false,
      equipmentOption: equipmentOption.call(this),
      // 物流能力
      logisticsList: [],
      logisticsLoading: false,
      logisticsOption: logisticsOption.call(this)
    }
  },
  methods: {
    onEquipmentLoad() {
      this.equipmentLoading = true
      getEquipmentList({ supplierId: this.id }).then(res => {
        this.equipmentList = res.data.data.records
        this.equipmentLoading = false
      })
    },
    refreshEquipmentChange() {
      this.onEquipmentLoad()
    },
    rowEquipmentSave(row, done, loading) {
      addEquipment({ ...row, supplierId: this.id }).then(
        () => {
          this.onEquipmentLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowEquipmentUpdate(row, index, done, loading) {
      updateEquipment(row).then(
        () => {
          this.onEquipmentLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowEquipmentDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeEquipment(row.id)
        })
        .then(() => {
          this.onEquipmentLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    // 物流能力
    onLogisticsLoad() {
      this.logisticsLoading = true
      getLogisticsList({ supplierId: this.id }).then(res => {
        this.logisticsList = res.data.data.records
        this.logisticsLoading = false
      })
    },
    refreshLogisticsChange() {
      this.onLogisticsLoad()
    },
    rowLogisticsSave(row, done, loading) {
      addLogistics({ ...row, supplierId: this.id }).then(
        () => {
          this.onLogisticsLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowLogisticsUpdate(row, index, done, loading) {
      updateLogistics(row).then(
        () => {
          this.onLogisticsLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowLogisticsDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeLogistics(row.id)
        })
        .then(() => {
          this.onLogisticsLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
